package com.zzy.controller;

import com.zzy.model.AjaxMsg;
import com.zzy.model.ScheduledCron;
import com.zzy.service.ScheduledCronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * html/scheduletask/list.html 测试
 */
@Controller
@RequestMapping("/scheduled/")
public class TaskController {
    @Autowired
    private ApplicationContext context;
    @Autowired
    private ScheduledCronService ScheduledCronService;
    /**
     * 查看任务列表
     */
    @RequestMapping("list")
    @ResponseBody
    public AjaxMsg taskList() {
        List<ScheduledCron> list =  ScheduledCronService.findAll();
        return AjaxMsg.returnAjaxMsg(list);
    }



    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("add")
    public AjaxMsg addTaskCron(ScheduledCron model) {
        ScheduledCronService.save(model);
        return AjaxMsg.returnAjaxMsg();
    }

    @ResponseBody
    @RequestMapping("get")
    public ScheduledCron editTaskCron(Integer id) {
        return  ScheduledCronService.findByID(id);
    }
    /**
     * 编辑任务cron表达式
     */
    @ResponseBody
    @RequestMapping("update")
    public AjaxMsg editTaskCron(ScheduledCron model ) {
        if(model!=null){
            ScheduledCronService.update(model);
        }
        return AjaxMsg.returnAjaxMsg();
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("delete")
    public AjaxMsg delete(Integer id) {
        if(id!=null){
            ScheduledCronService.delete(id);
        }
        return AjaxMsg.returnAjaxMsg();
    }

    /**
     * 启用或禁用定时任务
     */
    @ResponseBody
    @RequestMapping("change/status")
    public AjaxMsg changeStatusTaskCron(Integer status, String cronKey) {
        //cronRepository.updateStatusByCronKey(status, cronKey);
        ScheduledCron model = new ScheduledCron();
        ScheduledCronService.update(model);
        return AjaxMsg.returnAjaxMsg();
    }


    /**
     * 执行定时任务
     */
    @ResponseBody
    @RequestMapping("run/task")
    public AjaxMsg runTaskCron(String cronKey) throws Exception {
        return AjaxMsg.returnAjaxMsg();
    }


}
